﻿using System;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;

namespace MongoSample
{
    public class Db
    {
        private static readonly string _connectionString = "Server=127.0.0.1";

        private static readonly string _dbName = "MyNorthwind";

        private MongoDatabase database;
        public Db()
        {
            var connectionString = "mongodb://localhost";
            var client = new MongoClient(connectionString);
            var server = client.GetServer();
            database = server.GetDatabase("test");
        }

        public void RunCommand()
        {
            var comm = new CommandDocument
                {
                    {"text","customers"},
                    {"search","中文"},

                };
            var collection = database.GetCollection<Customer>("customers");
            CommandResult result = database.RunCommand(comm);
            
        }


        public void AllCust()
        {
            var collection = database.GetCollection<Customer>("customers");

            var customers = collection.AsQueryable<Customer>().ToList();

        }
        

        public ObjectId InsertCustomer(Customer customer)
        {
            var collection = database.GetCollection<Customer>("customers");
            collection.Insert(customer);
            var id = customer.Id;
            return id;
        }

    }
}